package vue;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import controleur.MainControleur;

public class winNouveauTrain extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	GridBagConstraints Contraints = null;
	Container Conteneur = null;
	GridBagLayout Schema = null;
	
	/* TEXTBOX*/
	JTextField txtType = new JTextField();
	JTextField txtNumero = new JTextField();
	JTextField txtHeure = new JTextField();
	JTextField txtDestProv = new JTextField();
	JTextField txtInfo = new JTextField();
	JTextField txtNumvoie = new JTextField();
	
	String  type = "";
	
	public winNouveauTrain(String[] donnees, String type)
	{
		this.type = type;
		
		Conteneur = this.getContentPane();
		Schema = new GridBagLayout();
		Conteneur.setLayout(Schema);
		
		Contraints = new GridBagConstraints();
		
		this.InitialiseComponents();
		
		txtType.setText(donnees[0]);
		txtNumero.setText(donnees[1]);
		txtHeure.setText(donnees[2]);
		txtDestProv.setText(donnees[3]);
		txtInfo.setText(donnees[4]);
		txtNumvoie.setText(donnees[5]);
		
		this.setSize(400, 300);
		this.setVisible(true);
	}
	
	private void InitialiseComponents()
	{
		/* JLABELS */
		String[] labelsTextes = {
				"Type", 
				"Numéro",  
				"Heure du départ / d'arrivée", 
				"Destination / Provenance", 
				"Info", 
				"Numéro de la voie"
		};
		
		JLabel lblBase = null;
		Contraints.fill = GridBagConstraints.CENTER;
		for(int i = 0; i < labelsTextes.length; i++)
		{
			lblBase = new JLabel(labelsTextes[i]);
			lblBase.setHorizontalAlignment(JLabel.RIGHT);
			this.AjouterComponent(lblBase, 0, i, 1, 1, 0.2, 0.5);
		}
		
		Contraints.fill = GridBagConstraints.BOTH;
		this.AjouterComponent(txtType, 1, 0, 1, 1, 0.5, 1);
		this.AjouterComponent(txtNumero, 1, 1, 1, 1, 0.5, 1);
		this.AjouterComponent(txtHeure, 1, 2, 1, 1, 0.5, 1);
		this.AjouterComponent(txtDestProv, 1, 3, 1, 1, 0.5, 1);
		this.AjouterComponent(txtInfo, 1, 4, 1, 1, 0.5, 1);
		this.AjouterComponent(txtNumvoie, 1, 5, 1, 1, 0.5, 1);
		
		/* JBUTTONS */
		JButton btnFermer = new JButton("Fermer");
		btnFermer.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		
		JButton btnEnregistrer = new JButton("Enregistrer");
		btnEnregistrer.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if("arr".equals(type))	{
					MainControleur.modiTrainArr(txtType.getText(),
							Integer.parseInt(txtNumero.getText()),
							txtHeure.getText(),
							txtDestProv.getText(),
							txtInfo.getText(),
							Integer.parseInt(txtNumvoie.getText())); 
					nettoyerTextBoxes();
				}
				else	{
					MainControleur.modiTrainDep(txtType.getText(),
							Integer.parseInt(txtNumero.getText()),
							txtHeure.getText(),
							txtDestProv.getText(),
							txtInfo.getText(),
							Integer.parseInt(txtNumvoie.getText())); 
					nettoyerTextBoxes();
				}
			}
		});
		
		Contraints.fill = GridBagConstraints.EAST;
		this.AjouterComponent(btnFermer, 0, 10, 1, 1, 0, 0);
		this.AjouterComponent(btnEnregistrer, 1, 10, 1, 1, 0, 0);
		
		this.setSize(400, 300);
		this.setResizable(false);
	}
	
	
	/**
	 * Ajouter un composant
	 * 
	 * @param sender
	 * @param gridCol
	 * @param gridRow
	 * @param colSpan
	 * @param rowSpan
	 * @param growHeight
	 * @param growWide
	 */
	private void AjouterComponent(Component sender, int gridCol, int gridRow, int colSpan, int rowSpan, double growHeight, double growWide)
	{
		Contraints.gridx = gridCol;
		Contraints.gridy = gridRow;
		Contraints.gridwidth = colSpan;
		Contraints.gridheight = rowSpan;
		Contraints.weighty = growHeight;
		Contraints.weightx = growWide;
		Schema.setConstraints(sender, Contraints);
		Conteneur.add(sender);
	}
	
	private void nettoyerTextBoxes()
	{
		JTextField[] boxes = { 
				txtType = new JTextField(),
				txtNumero = new JTextField(),
				txtHeure = new JTextField(),
				txtDestProv = new JTextField(),
				txtInfo = new JTextField(),
				txtNumvoie = new JTextField()
		};
		for(JTextField unBox : boxes)
		{
			unBox.setText("");
		}
	}
}
